﻿using SendGrid.Transport;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Web;

namespace SpaceNeedle.City6473.Web.Data.Helpers
{
    public class Mail
    {
        public static string GetTemplate()
        {
            return @"<!doctype html><html><head></head><body style=""padding:0px;overflow:hidden;font-family:Segoe UI, Arial;font-size:14px;"">" +
            @"<div style=""background-color:#fe4200; height:10px; width:100%;"">&nbsp;</div>" +
            @"<div style=""padding:40px;><a href=""http://www.e-opinion.net"" target=""_blank""><img src=""http://www.e-opinion.net/Content/Images/e-opinion_150.png"" style=""border:0px;width:150px;""/></a><br/><br/>" +
            "<div>{{content}}</div>" +
            "<br/><br/><br/></div></body></html>";
        }

        public static void Send(string to, string subject, string body)
        {
            Send(new string[]{ to }, subject, body);
        }

        public static void Send(string[] to, string subject, string body)
        {
              body = GetTemplate().Replace("{{title}}", subject).Replace("{{content}}", body);
              SendViaSendgrid(to, "murilo.curti@live.com", subject, body);
        }

        public static void SendViaSendgrid(string[] to,string from, string subject, string body)
        {
            try
            {
                MailMessage mailMsg = new MailMessage();

                // To
                foreach (var item in to)
                {
                    mailMsg.To.Add(new MailAddress(item));
                }
                

                // From
                mailMsg.From = new MailAddress(from);

                // Subject and multipart/alternative Body
                mailMsg.Subject = subject;
                string text = body;
                string html = body;
                mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
                mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));

                // Init SmtpClient and send
                SmtpClient smtpClient = new SmtpClient("smtp.sendgrid.net", Convert.ToInt32(587));
                System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("eopinion", "@adfs$%sdf212");
                smtpClient.Credentials = credentials;

                smtpClient.Send(mailMsg);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

        public static void SendViaHotmail(string to, string subject, string body)
        {
            try
            {
                SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
                var mail = new MailMessage();
                mail.From = new MailAddress("worker@e-opinion.net");
                mail.To.Add(to);
                mail.Subject = subject;
                mail.IsBodyHtml = true;
                mail.Body = body;
                SmtpServer.Port = 587;
                SmtpServer.UseDefaultCredentials = false;
                SmtpServer.Credentials = new System.Net.NetworkCredential("worker@e-opinion.net", "e@hard$work3#");
                SmtpServer.EnableSsl = true;
                SmtpServer.Send(mail);
            }
            catch { }
        }
    }
}